package com.sctvcloud.qingchuan.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.beans.NewsItem;
import com.sctvcloud.qingchuan.beans.TopicSubBean;
import com.sctvcloud.qingchuan.ui.activities.SubTopicActivity;
import com.sctvcloud.qingchuan.ui.utils.SkipUtil;
import com.sctvcloud.qingchuan.ui.utils.UIUtils;

import java.util.HashMap;
import java.util.List;

/**
 * @author suming
 * @date 2020/8/28 18:23
 */
public class TopicNewsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TITLE = 502;
    public static final int CONTEXT = 709;

    private List<NewsItem> news;
    private Context context;

    public TopicNewsListAdapter(Context context, List<NewsItem> news) {
        this.news = news;
        this.context = context;
    }

    public void setNews(List<NewsItem> news) {
        this.news = news;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (news.get(position).isTitle()) {
            return TITLE;
        } else {
            return CONTEXT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TITLE) {
            return new TitleViewHolder(LayoutInflater.from(context).inflate(R.layout.item_topic_title, parent, false));
        } else {
            return new ContentViewHolder(LayoutInflater.from(context).inflate(R.layout.topic_news_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TITLE) {
            TitleViewHolder holder1 = (TitleViewHolder) holder;
            final NewsItem data = news.get(position);
            holder1.newsTitle.setText(data.getNewsTitle());
            holder1.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SubTopicActivity.start(new TopicSubBean("0", data.getNewsTitle(), data.getNewsUrl()));
                }
            });
        } else {
            ContentViewHolder holder2 = (ContentViewHolder) holder;
            final NewsItem data = news.get(position);
            holder2.newsTitle.setText(data.getNewsTitle());
            holder2.clickcount.setVisibility(View.VISIBLE);
            holder2.clickcount.setText(UIUtils.getReadingNumber(data.getReadCount()));
            holder2.newsTime.setText(DateUtils.formatDate(DateUtils.getDataDate(data), ""));
            holder2.layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SkipUtil.skipWithNewsItem(context, data, new HashMap<String, String>());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return news.size();
    }

    static class ContentViewHolder extends RecyclerView.ViewHolder {
        LinearLayout layout;
        CustomFontTextView newsTitle;
        CustomFontTextView newsTime;
        CustomFontTextView clickcount;

        public ContentViewHolder(View view) {
            super(view);
            layout = view.findViewById(R.id.news_item_layout);
            newsTitle = view.findViewById(R.id.news_title);
            newsTime = view.findViewById(R.id.news_time);
            clickcount = view.findViewById(R.id.newes_detail_click_count);
        }
    }

    static class TitleViewHolder extends RecyclerView.ViewHolder {
        TextView newsTitle;
        TextView more;

        public TitleViewHolder(View view) {
            super(view);
            newsTitle = view.findViewById(R.id.title);
            more = view.findViewById(R.id.more);
        }
    }
}
